package cn.edu.xhu.library_management.user.service.impl;

import cn.edu.xhu.library_management.user.entity.search.StudentSearch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.xhu.library_management.user.entity.LmUserStudent;
import cn.edu.xhu.library_management.user.service.LmUserStudentService;
import cn.edu.xhu.library_management.user.mapper.LmUserStudentMapper;
import org.springframework.stereotype.Service;

/**
* @author 21086
* @description 针对表【lm_user_student(学生表)】的数据库操作Service实现
* @createDate 2023-04-18 08:58:28
*/
@Service
public class LmUserStudentServiceImpl extends ServiceImpl<LmUserStudentMapper, LmUserStudent>
    implements LmUserStudentService{


    @Override
    public Page<LmUserStudent> search(StudentSearch search) {
        Page<LmUserStudent> page = new Page<>(search.getCurrentPage(), search.getPageSize());
        QueryWrapper<LmUserStudent> wrapper = searchCondition(search);
        return this.page(page,wrapper);
    }

    /**
     * 搜索条件
     * @param search 搜索对象
     * @return 查询构造器
     */
    private QueryWrapper<LmUserStudent> searchCondition(StudentSearch search){
        QueryWrapper<LmUserStudent> wrapper = new QueryWrapper<>();
        if (search.getStAccount() != null) {
            // 用户有账号
            wrapper.lambda().eq(LmUserStudent::getStAccount, search.getStAccount());
        }
        if (search.getStName() != null) {
            // 姓名模糊查询
            wrapper.lambda().like(LmUserStudent::getStName, search.getStName());
        }
        if (search.getStClass() != null) {
            // 班级查询学生
            wrapper.lambda().eq(LmUserStudent::getStClass, search.getStClass());
        }
        if (search.getStIslock() != null) {
            // 是否锁定
            wrapper.lambda().eq(LmUserStudent::getStIslock, search.getStIslock());
        }
        if (search.getStIsschool() != null) {
            // 是否在校
            wrapper.lambda().eq(LmUserStudent::getStIsschool, search.getStIsschool());
        }
        if (search.getStMinBalance() != null && search.getStMaxBalance() != null){
            // 最小最大余额范围
            wrapper.lambda().between(LmUserStudent::getStBalance,search.getStMinBalance(),search.getStMaxBalance());
        } else{
            if (search.getStMinBalance() != null) {
                // 大于等于
                wrapper.lambda().ge(LmUserStudent::getStBalance,search.getStMinBalance());
            }else if (search.getStMaxBalance() != null){
                // 小于等于
                wrapper.lambda().le(LmUserStudent::getStBalance,search.getStMaxBalance());
            }
        }
        if (search.getStMinBorrowNum() != null && search.getStMaxBorrowNum() != null){
            // 最小最大借阅范围
            wrapper.lambda().between(LmUserStudent::getStBorrowNum,search.getStMinBorrowNum(),search.getStMaxBorrowNum());
        } else{
            if (search.getStMinBorrowNum() != null) {
                // 大于等于
                wrapper.lambda().ge(LmUserStudent::getStBorrowNum,search.getStMinBorrowNum());
            }else if (search.getStMaxBorrowNum() != null){
                // 小于等于
                wrapper.lambda().le(LmUserStudent::getStBorrowNum,search.getStMaxBorrowNum());
            }
        }
        return wrapper;
    }
}




